package org.jeecg.modules.demo.upload.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.upload.entity.ZyUpload;
import org.jeecg.modules.demo.upload.service.ZyUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @program: git-zhihui-backen
 * @description:
 * @author: 张浩
 * @create: 2023-06-16 21:22
 **/
@Api(tags="文件上传")
@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {

    @Autowired
    private ZyUploadService zyUploadService;

    @AutoLog(value = "上传文件")
    @ApiOperation(value="上传文件", notes="上传文件")
    @PostMapping(value = "/file")
    public Result<String> add(MultipartFile file){
        return zyUploadService.add(file);
    }

    @AutoLog(value = "上传项目图片")
    @ApiOperation(value="上传项目图片", notes="上传项目图片")
    @PostMapping(value = "/fileone")
    public Result<String> addphoto(MultipartFile file){
        return zyUploadService.addFile(file);
    }

    @AutoLog(value = "上传bim")
    @ApiOperation(value="上传bim", notes="上传bim")
    @PostMapping(value = "/filebim")
    public Result<Map<String,String>> addBim(MultipartFile file, String name){
        return zyUploadService.add(file,name);
    }

    @AutoLog(value = "获取子文件夹")
    @ApiOperation(value="获取子文件夹", notes="获取子文件夹")
    @PostMapping(value = "/subfolder")
    public Result<List<String>> subfolder(){
        return zyUploadService.listFolder();
    }

    /**
     * 分页列表查询
     *
     * @param zyUpload
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "zy_upload-分页列表查询")
    @ApiOperation(value="zy_upload-分页列表查询", notes="zy_upload-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ZyUpload>> queryPageList(ZyUpload zyUpload,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<ZyUpload> queryWrapper = QueryGenerator.initQueryWrapper(zyUpload, req.getParameterMap());
        Page<ZyUpload> page = new Page<ZyUpload>(pageNo, pageSize);
        IPage<ZyUpload> pageList = zyUploadService.page(page, queryWrapper);
        List<ZyUpload> records = pageList.getRecords();
        System.out.println("records = " + records);
        return Result.OK(pageList);
    }

    @AutoLog(value = "师兄图片上传")
    @ApiOperation(value="师兄图片上传", notes="师兄图片上传")
    @GetMapping(value = "/uploadScenePhoto")
    public Result<String> uploadScenePhoto(MultipartFile file) throws Exception {
        String stringResult = zyUploadService.uploadSceneFile(file);
        return Result.OK(stringResult);
    }

    /**
     * 查询2
     *
     * @param zyUpload
     * @param req
     * @return
     */
    //@AutoLog(value = "zy_upload-分页列表查询")
    @ApiOperation(value="zy_upload-分页列表查询", notes="zy_upload-分页列表查询")
    @GetMapping(value = "/list1")
    public Result<List<ZyUpload>> queryPageList1(ZyUpload zyUpload,
                                                 HttpServletRequest req) {
        QueryWrapper<ZyUpload> queryWrapper = QueryGenerator.initQueryWrapper(zyUpload, req.getParameterMap());
        List<ZyUpload> list = zyUploadService.list(queryWrapper);
        return Result.OK(list);
    }


    /**
     *   添加
     *
     * @param zyUpload
     * @return
     */
    @AutoLog(value = "文件上传-添加")
    @ApiOperation(value="文件上传-添加", notes="文件上传-添加")
//    @RequiresPermissions("upload1:zy_upload:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ZyUpload zyUpload) {
        zyUploadService.save(zyUpload);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param zyUpload
     * @return
     */
    @AutoLog(value = "文件上传-编辑")
    @ApiOperation(value="文件上传-编辑", notes="文件上传-编辑")
//    @RequiresPermissions("upload1:zy_upload:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody ZyUpload zyUpload) {
        zyUploadService.updateById(zyUpload);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文件上传-通过id删除")
    @ApiOperation(value="文件上传-通过id删除", notes="文件上传-通过id删除")
//    @RequiresPermissions("upload1:zy_upload:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        zyUploadService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文件上传-批量删除")
    @ApiOperation(value="文件上传-批量删除", notes="文件上传-批量删除")
//    @RequiresPermissions("upload1:zy_upload:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.zyUploadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "文件上传-通过id查询")
    @ApiOperation(value="文件上传-通过id查询", notes="文件上传-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ZyUpload> queryById(@RequestParam(name="id",required=true) String id) {
        ZyUpload zyUpload = zyUploadService.getById(id);
        if(zyUpload==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(zyUpload);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param zyUpload
     */
//    @RequiresPermissions("upload1:zy_upload:exportXls")
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, ZyUpload zyUpload) {
//        return super.exportXls(request, zyUpload, ZyUpload.class, "文件上传");
//    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("upload1:zy_upload:importExcel")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, ZyUpload.class);
//    }


}
